//
//  LGInspectReportDetailController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/2.
/*
 owner_upload ： Y 医生上传的报告
 type：LR - 检验报告 、CT_0001 - 心电图、 CT_0002 - 心脏彩超
*/

#import "LGInspectReportDetailController.h"
//报告详情
#import "LGInspectDetailDoctorController.h"
//HIS报告详情
#import "LGReportDetailHisController.h"
#import "LGReportDetailItemCell.h"

#define viewHeight  (Screen_H-topBarHeight-viewPix(120))

@interface LGInspectReportDetailController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
/**   */
@property (nonatomic , strong)UICollectionView *collectionView;
/**   */
@property (nonatomic , strong)UIPageControl *pageControl;
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGInspectReportDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"报告详情";
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.pageControl];
    [self.view addSubview:self.scrollView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Visitrecords/reportRecordSummary" parameters:@{@"patient_id":self.patientid,@"report_date":self.reportDate,@"data_type":self.data_type} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dataArray = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    NSInteger index = 0;
    for (NSInteger i=0; i<dataArray.count; i++) {
        NSDictionary *tempDic = dataArray[i];
        NSString *doctor = LGNSString(tempDic[@"owner_upload"]);
        NSString *type = LGNSString(tempDic[@"type"]);
        NSString *recordid = LGNSString(tempDic[@"record_id"]);
        index = [recordid isEqualToString:self.recordid]?i:index;
        //心康管家新改的
        NSArray *detail = tempDic[@"report_detail"];
        if (detail.count>0) {
            LGReportDetailHisController *controller = [[LGReportDetailHisController alloc]init];
            controller.view.frame = CGRectMake(Screen_W*i, 0, Screen_W, viewHeight);
            controller.dataDic = tempDic;
            [self addChildViewController:controller];
            [self.scrollView addSubview:controller.view];
        }else{
            //纯图片
            LGInspectDetailDoctorController *photoVC = [[LGInspectDetailDoctorController alloc]init];
            photoVC.view.frame = CGRectMake(Screen_W*i, 0, Screen_W, viewHeight);
            photoVC.patientNum = tempDic[@"registration_no"];
            photoVC.patientid = tempDic[@"patient_id"];
            photoVC.photoArray = tempDic[@"files"];
//            photoVC.anomalyArray = tempDic[@"items"];
            [self addChildViewController:photoVC];
            [self.scrollView addSubview:photoVC.view];
        }
        
        //
//        if([doctor isEqualToString:@"N"]){
//            //识别的
//            if([type isEqualToString:@"LR"]){
//                //检验报告
//                LGInspectDetailLRController *controller = [[LGInspectDetailLRController alloc]init];
//                controller.view.frame = CGRectMake(Screen_W*i, 0, Screen_W, viewHeight);
//                controller.dataArray = tempDic[@"items"];
//                controller.fileArray = tempDic[@"files"];
//                controller.patientNum = tempDic[@"registration_no"];
//                controller.reportName = LGNSString(tempDic[@"title"]);
//                [self addChildViewController:controller];
//                [self.scrollView addSubview:controller.view];
//                
//            }else if([type isEqualToString:@"CT_0001"]){
//                //心电图
//                LGInspectDetailCT01Controller *controller = [[LGInspectDetailCT01Controller alloc]init];
//                controller.view.frame = CGRectMake(Screen_W*i, 0, Screen_W, viewHeight);
//                controller.dataDic = tempDic[@"result"];
//                controller.filePath = LGNSString(tempDic[@"files"][0]);
//                controller.reportName = LGNSString(tempDic[@"title"]);
//                [self addChildViewController:controller];
//                [self.scrollView addSubview:controller.view];
//                
//            }else if([type isEqualToString:@"CT_0002"]){
//                //心脏彩超
//                LGInspectDetailCT02Controller *controller = [[LGInspectDetailCT02Controller alloc]init];
//                controller.view.frame = CGRectMake(Screen_W*i, 0, Screen_W, viewHeight);
//                controller.dataDic = tempDic[@"result"];
//                controller.filePath = LGNSString(tempDic[@"files"][0]);
//                controller.reportName = LGNSString(tempDic[@"title"]);
//                [self addChildViewController:controller];
//                [self.scrollView addSubview:controller.view];
//            }
//        }else{
//            //医生、患者自己上传的
//            LGInspectDetailDoctorController *photoVC = [[LGInspectDetailDoctorController alloc]init];
//            photoVC.view.frame = CGRectMake(Screen_W*i, 0, Screen_W, viewHeight);
//            photoVC.patientNum = tempDic[@"registration_no"];
//            photoVC.photoArray = tempDic[@"files"];
//            photoVC.anomalyArray = tempDic[@"items"];
//            [self addChildViewController:photoVC];
//            [self.scrollView addSubview:photoVC.view];
//        }
    }
    if (dataArray.count>0) {
        self.scrollView.contentSize = CGSizeMake(Screen_W*dataArray.count, viewHeight);
        [self.collectionView reloadData];
        self.pageControl.numberOfPages = dataArray.count;
        self.pageControl.currentPage = index;
        [self.scrollView setContentOffset:CGPointMake(Screen_W*index, 0) animated:NO];
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGReportDetailItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.dataDic = self.dataArray[indexPath.item];
    return cell;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if(scrollView == self.collectionView){
        NSInteger index = self.collectionView.contentOffset.x/viewPix(353);
        [self.scrollView setContentOffset:CGPointMake(Screen_W*index, 0) animated:YES];
        [self.pageControl setCurrentPage:index];
    }else{
        NSInteger index = self.scrollView.contentOffset.x/Screen_W;
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        [self.pageControl setCurrentPage:index];
    }
}


#pragma mark
#pragma mark ====> 创建控件
-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(viewPix(353), viewPix(86));
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake((Screen_W-viewPix(353))/2.0, viewPix(10)+topBarHeight, viewPix(353), viewPix(86)) collectionViewLayout:layout];
        [_collectionView registerClass:[LGReportDetailItemCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.pagingEnabled = YES;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.cornerRidus = 15;
    }
    return _collectionView;
}

-(UIPageControl *)pageControl{
    if(!_pageControl){
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, topBarHeight+viewPix(104), Screen_W, viewPix(15))];
        _pageControl.pageIndicatorTintColor = [UIColor colorWithString:@"#CBDCFB"];
        _pageControl.currentPageIndicatorTintColor = [UIColor colorWithString:@"#4C87FD"];
    }
    return _pageControl;
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, topBarHeight+viewPix(120), Screen_W, Screen_H-topBarHeight-viewPix(120))];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
